package labox.innovation.gameserver.model.actor.instance;

import labox.innovation.config.Config;
import labox.innovation.gameserver.network.serverpackets.GUIList;
import labox.innovation.gameserver.templates.chars.NpcTemplate;

/**
 * 兑换师NPC功能，目前元宝兑换成银文
 * @author Frank
 *
 */
public class FuzeExchangerInstance extends FuzeNpcInstance {
	
	private static final String requestHtml = "<form nm='takeinput' w='160' h='110'>\n"
											 + "<tb ln='n'>"
		                                     + " <tr><td>每1元宝可以兑换$note$银票，你当前有$ingots$元宝，请输入你要兑换的元宝数量</td></tr>\n"
	                                         + " <tr><td><i nm='takenum' t='n' w = '150'/></td></tr>\n"
	                                         + " <tr><td><a cmd='' oc='takesure()' type = 'button'>确定 </a></td>"
	                                         + " <td><a cmd='' oc='back()' type = 'button' pl = 'r'>取消 </a></td></tr></tb>\n"
	                                         + "        <s>\n"
	                                 		 + "            sub takesure()\n"
	                                 		 + "               num=getIS(\"takenum\")\n "
                                             + "               if(equals(num,\"\"))\n "
		                                     + "                   notice(\"请输入要兑换元宝数量\")\n "
                                             + "                   ret\n "
                                             + "               eif\n "
	                                 		 + "            action(\"s:85:bypass -h ExchangeItem \" + num)\n"
	                                 		 + "            back()\n"
	                                 		 + "            ret\n"
	                                 		 + "        </s>\n"
	                                         + "</form>\n";

	public FuzeExchangerInstance(long objectId, NpcTemplate template) {
		super(objectId, template);
	}
	
	public static boolean doExchange(FuzePcInstance player, int ingots) {
		boolean result = false;
				
		if (player.reduceMoneyYuanBao("元宝兑换银票", ingots, null, true)) {
			player.addMoneyNote("元宝兑换银票", ingots * Config.INGOT2SILVERRATE, null, true);
		}
		
		return result;
	}
	
	/**
	 * 下发兑换界面
	 */
	private void showExchangeWindow(FuzePcInstance player) {
		String  ingots = String.valueOf(player.getMoneyYuanBao());
		player.sendPacket(new GUIList(requestHtml.replace("$ingots$", ingots).replace("$note$", String.valueOf(Config.INGOT2SILVERRATE))));
	}

	@Override
	public void onBypassFeedback(FuzePcInstance player, String command) {
		if (command.startsWith("ShowExchangeWindow")) {
			showExchangeWindow(player);
		} else {
			super.onBypassFeedback(player, command);
		}
	}
}
